<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>_state的简写方式</title>
</head>
<body>
  <div id="test"></div>

  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

  <script type="text/babel">
    class Weather extends React.Component {
      constructor(props) {
        super(props)     
        // this.state = { isHot: false, wind: '风' }   
        // this.newWeather = this.changeWeather.bind(this)      
      }
      // 简写
      state = { isHot: false, wind: '风' }    // 往weather的实例对象的state上添加属性
      render() {
        return <h1 id='title' onClick= { this.changeWeather }>今天天气很{ this.state.isHot ? '炎热' : '凉爽' }</h1>
      }
      /* 
        箭头函数没有自己的this，他找其外层函数的this去作为自己的this
      */

      // 原本自定义的changeWeather方法是挂载在weather的原型对象上，但是这样用箭头函数赋值写后，changeWeather方法就挂在在weather的实例对象身上
      changeWeather = () => {
        const isHot = this.state.isHot
        this.setState({ isHot: !isHot })
        console.log(this)   // weather的实例对象
      }
    }
    ReactDOM.render(<Weather/>, document.getElementById('test'))
  </script>
</body>
</html>